package org.jeecg.modules.crawler.job;

import cn.hutool.http.HtmlUtil;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.ResponseBody;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.jeecg.common.util.DateTimeUtil;
import org.jeecg.modules.wechat.entity.SeuWx;
import org.jeecg.modules.wechat.service.ISeuWxService;
import org.jeecg.modules.weibo.entity.SeuWb;
import org.jeecg.modules.weibo.service.ISeuWbService;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 任务定时发送
 */
@Slf4j
public class SeuWBCrawlerJob implements Job {

	@Autowired
	private ISeuWbService seuWbService;



	@Override
	public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
		try {
			HttpClient client = HttpClientBuilder.create().build();

			HttpGet request = new HttpGet("https://m.weibo.cn/api/container/getIndex?t=0&luicode=10000011&lfid=231583&type=uid&value=1703010470&containerid=1076031703010470");

			// 设置请求头
			request.addHeader("Content-Encoding", "gzip");
			request.addHeader("Content-Type", "application/json; charset=utf-8");
			request.addHeader("Proc_node", "mapi-weibopro-node-bypass-upgrade-beta-575d67d6fc-qq4fc");
			request.addHeader("Vary", "Origin");
			request.addHeader("X-Content-Type-Options", "nosniff");
			request.addHeader("X-Download-Options", "noopen");
			request.addHeader("X-Frame-Options", "SAMEORIGIN");
			request.addHeader("X-Log-Uid", "2014155935235");
			request.addHeader("X-Xss-Protection", "1; mode=block");

			// 执行请求
			HttpResponse response = client.execute(request);

			// 处理响应
			String responseBody = EntityUtils.toString(response.getEntity());
			System.out.println(responseBody);
			ObjectMapper mapper = new ObjectMapper();
			Map<String, Object> responseMap = mapper.readValue(responseBody, Map.class);
			Map<String,Object> dataMap = (Map<String,Object>)responseMap.get("data");
			List<Map<String,Object>> listMap = (List<Map<String,Object>>)dataMap.get("cards");
//			List<SeuWb> wbList =  new ArrayList<>();
			for(Map<String,Object> tempMap : listMap){
				Map<String,Object> blog = (Map<String,Object>)tempMap.get("mblog");
//				System.out.println(HtmlUtil.cleanHtmlTag(blog.get("text").toString()));
				SeuWb wb = new SeuWb();
				wb.setText(HtmlUtil.cleanHtmlTag(blog.get("text").toString()));
				wb.setTime(transferTime(blog.get("created_at").toString()));
				try {
					seuWbService.saveOrUpdate(wb);
				}catch (Exception e){
					e.printStackTrace();
				}
//				wbList.add(wb);
			}
			// 确保释放资源
//			seuWbService.saveOrUpdateBatch(wbList);

			EntityUtils.consume(response.getEntity());

		} catch (Exception e) {
			e.printStackTrace();
		}
	}



	public  static String transferTime(String in){

		try {
			SimpleDateFormat inputFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss Z yyyy", Locale.ENGLISH);
			SimpleDateFormat outputFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

			Date date = inputFormat.parse(in);
			String formattedDate = outputFormat.format(date);
//			System.out.println(formattedDate);  // 输出: 2024-05-31 19:34:53
			return formattedDate;
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return "";
	}


}
